﻿using AutoMapper;
using Equipment.API.DTOs.RequestDto.DefectlocationListRequest;
using Equipment.API.DTOs.ResponseDto.DefectlocationResponse;
using Equipment.Domain.Detectionmethod;
using Equipment.Errorcode;
using Equipment.Errorcode.ErrorCode;
using Equipment.Infrastructure.Interfaces;

namespace Equipment.API.Application.Service.DetectionmethodService
{
    /// <summary>
    /// 缺陷位置信息服务层
    /// </summary>
    public class DefectlocationService
    {
        private readonly IDefectlocationRepository _defectionsRepository;

        private readonly IMapper _mapper;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="defectionsRepository"></param>
        /// <param name="mapper"></param>
        public DefectlocationService(IDefectlocationRepository defectionsRepository, IMapper mapper)
        {
            _defectionsRepository = defectionsRepository;
            _mapper = mapper;
        }
        /// <summary>
        /// 缺陷位置信息下拉框
        /// </summary>
        /// <returns></returns>
        public async Task<ApiResult> GetDefectlocationDropdown()
        {
            var restult = await _defectionsRepository.GetAllAsync();

            var restultDto = _mapper.Map<List<DefectlocationDropdownResponseDto>>(restult);

            return ApiResult.Ok(restultDto, "缺陷位置信息获取成功！");
        }

        /// <summary>
        /// 新增缺陷位置信息
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<ApiResult> AddDefectlocation(AddDefectlocationResponseDto param)
        {
            var result = _mapper.Map<Defectlocation>(param);
            var defections = await _defectionsRepository.InsertAsync(result);

            return ApiResult.Ok(defections, "缺陷位置信息添加成功！");
        }

        /// <summary>
        /// 获取缺陷位置信息列表
        /// </summary>
        /// <returns></returns>
        public async Task<ApiResult> GetDefectlocationList(int pageNumber = 1, int pageSize = 2)
        {
            var result = (await _defectionsRepository.GetAllAsync(a => a.Isdelete == false)).AsQueryable();

            var pageResult = PaginationHelper.Paginate(result, pageNumber, pageSize); 
            return ApiResult.Ok(pageResult, "缺陷位置信息显示成功！");
        }

        /// <summary>
        /// 修改缺陷位置信息
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<ApiResult> UpdateDefectlocation(UpdateDefectlocationRequestDto param)
        {
            var update = await _defectionsRepository.GetAsync(a => a.Defectlocationid == param.Defectlocationid);
            if (update == null)
            {
                return ApiResult.Fail("缺陷位置信息不存在！");
            }
            var entity = _mapper.Map(param, update);
            await _defectionsRepository.UpdateAsync(entity);

            return ApiResult.Ok(update, "缺陷位置信息更新成功！");
        }

        /// <summary>
        /// 删除缺陷位置信息
        /// </summary>
        /// <param name="Defectlocationid"></param>
        /// <returns></returns>
        public async Task<ApiResult> DeleteDefectlocation(int Defectlocationid)
        {
            var entity = await _defectionsRepository.GetAsync(a => a.Defectlocationid == Defectlocationid);
            if (entity == null)
            {
                return ApiResult.Fail("缺陷位置信息不存在！");
            }
            entity.Isdelete = true;
            await _defectionsRepository.UpdateAsync(entity);
            return ApiResult.Ok(entity, "缺陷位置信息删除成功！");
        }

    }

}
